package com.yiwei.hdb.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.yiwei.hdb.R;


class LabelContext {
    private  float iconTextPadding;
    private Bitmap bitmap;
    private int iconLeft;
    private float iconWidth;
    private float iconHeight;
    private View mParent;
    private Rect bitmapRect;
    private float mBaseline;
    private float mLabelX;
    private Paint mLabelPaint;
    private String mLabelText;
    private ColorStateList mLabelColor;

    private int mPaddingRight = 0;
    private float mSuffixWidth = 0;
    private Paint mSuffixPaint;
    private Paint mBitmapPaint;
    private String mSuffixText;
    private ColorStateList mSuffixColor;

    LabelContext(Context context, View parent, AttributeSet attrs) {
        this.mParent = parent;
        TypedArray a = parent.getContext().obtainStyledAttributes(attrs, R.styleable.LabelEditText);
        float defaultTextSize = parent instanceof TextView
                ? ((TextView) parent).getTextSize()
                : TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, Resources.getSystem().getDisplayMetrics());
        float labelSize = defaultTextSize, suffixSize = defaultTextSize;
        float labelWidth = 0, suffixWidth = 0;
        int labelStyle = -1, suffixStyle = -1;
        int labelTypeface = -1, suffixTypeface = -1;
        String labelTypefacePath = null, suffixTypefacePath = null;
        try {
            mLabelText = a.getString(R.styleable.LabelEditText_labelText);
            mLabelColor = a.getColorStateList(R.styleable.LabelEditText_labelColor);
            labelSize = a.getDimension(R.styleable.LabelEditText_labelSize, labelSize);
            labelWidth = a.getDimension(R.styleable.LabelEditText_labelWidth, labelWidth);
            labelStyle = a.getInteger(R.styleable.LabelEditText_labelStyle, -1);
            labelTypefacePath = a.getString(R.styleable.LabelEditText_labelTypeface);
            iconLeft = a.getResourceId(R.styleable.LabelEditText_lableIconLeft, 0);
            iconWidth = a.getDimension(R.styleable.LabelEditText_lableIconWidth, 0);
            iconHeight = a.getDimension(R.styleable.LabelEditText_lableIconHeight, 0);
            iconTextPadding=a.getDimension(R.styleable.LabelEditText_lableIconTextPadding,0);
            try {
                labelTypeface = a.getInteger(R.styleable.LabelEditText_labelTypeface, -1);
            } catch (Exception e) {

            }
            mSuffixText = a.getString(R.styleable.LabelEditText_suffixText);
            mSuffixColor = a.getColorStateList(R.styleable.LabelEditText_suffixColor);
            suffixSize = a.getDimension(R.styleable.LabelEditText_suffixSize, suffixSize);
            mSuffixWidth = a.getDimension(R.styleable.LabelEditText_suffixWidth, suffixWidth);
            suffixStyle = a.getInteger(R.styleable.LabelEditText_suffixStyle, -1);
            suffixTypefacePath = a.getString(R.styleable.LabelEditText_suffixTypeface);
            try {
                suffixTypeface = a.getInteger(R.styleable.LabelEditText_suffixTypeface, -1);
            } catch (Exception e) {

            }
        } finally {
            a.recycle();
        }

        mLabelX = parent.getPaddingLeft();
        mLabelPaint = PaintUtil.createTextPaint(parent.getContext(), labelSize, labelStyle, labelTypeface, labelTypefacePath);
        setPaintColor(mLabelPaint, mLabelColor);
        mSuffixPaint = PaintUtil.createTextPaint(parent.getContext(), suffixSize, suffixStyle, suffixTypeface, suffixTypefacePath);
        setPaintColor(mSuffixPaint, mSuffixColor);
        mPaddingRight = parent.getPaddingRight();
        parent.setPadding(parent.getPaddingLeft() + (int) labelWidth, parent.getPaddingTop(), parent.getPaddingRight() + (int) mSuffixWidth, parent.getPaddingBottom());


        if (iconLeft != 0) {
            mBitmapPaint = new Paint();
//            bitmapRect =
            bitmap = BitmapFactory.decodeResource(parent.getResources(), iconLeft);
        }
    }

    void setLabelText(String value) {
        mLabelText = value;
        mParent.invalidate();
    }

    void setSuffixText(String value) {
        mSuffixText = value;
        mParent.invalidate();
    }

    private void setPaintColor(Paint paint, ColorStateList textColor) {
        if (paint == null) {
            return;
        }
        if (textColor != null) {
            paint.setColor(textColor.getColorForState(mParent.getDrawableState(), 0));
        } else if (mParent instanceof TextView) {
            paint.setColor(((TextView) mParent).getCurrentTextColor());
        }
    }

    void drawableStateChanged() {
        setPaintColor(mLabelPaint, mLabelColor);
        setPaintColor(mSuffixPaint, mSuffixColor);
    }

    void onDraw(Canvas canvas, int measureWidth, int measureHeight) {
        if (iconLeft != 0) {
            canvas.drawBitmap(bitmap, null,
                    new Rect((int) mLabelX, (int) (measureHeight*0.5 - iconHeight*0.5), (int) (mLabelX + iconWidth), (int) (measureHeight*0.5 + iconHeight * 0.5)),
                     mBitmapPaint);
        }
        if (mBaseline == 0) {
            Paint.FontMetrics metrics = mLabelPaint.getFontMetrics();
            mBaseline = (mParent.getMeasuredHeight() - metrics.bottom - metrics.top) / 2;
        }
        if (mLabelText != null) {
            if (mLabelText.contains("*")) {
                mLabelPaint.setColor(0xFFDF2127);
                canvas.drawText("*", mLabelX + mParent.getScrollX()+iconTextPadding+iconWidth, mBaseline, mLabelPaint);
                mLabelPaint.setColor(mLabelColor.getDefaultColor());
                canvas.drawText(mLabelText.replace("*", ""), mLabelPaint.measureText("*") + mLabelX + mParent.getScrollX()+iconTextPadding+iconWidth, mBaseline, mLabelPaint);
            } else {
                canvas.drawText(mLabelText, mLabelX + mParent.getScrollX()+iconTextPadding+iconWidth, mBaseline, mLabelPaint);
            }

        }
        if (mSuffixText != null) {
            Rect rect = new Rect();
            mSuffixPaint.getTextBounds(mSuffixText, 0, mSuffixText.length(), rect);
            int suffixX = mParent.getMeasuredWidth() - mPaddingRight - rect.width();
            canvas.drawText(mSuffixText, suffixX, mBaseline, mSuffixPaint);
        }
    }
}